package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.AlbumPhotoDTO;
import com.jyh.blog.dto.PhotoBackDTO;
import com.jyh.blog.service.PhotoService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "照片管理")
@RestController
public class PhotoController {
    @Autowired
    private PhotoService photoService;

    /**
     * 获取首页相册图片
     * @return
     */
    @ApiOperation(value = "获取首页相册照片")
    @GetMapping("/albums/{id}/photos")
    public Result<AlbumPhotoDTO> photoOfAlbumList(@PathVariable("id") String id){
        return Result.ok(photoService.photoOfAlbumList(id));
    }


    /**
     * 保存照片
     * @param photoVO
     * @return
     */
    @OptLog(optType = SAVE)
    @ApiOperation(value = "保存照片")
    @PostMapping("/admin/photo")
    public Result<String> savePhotos(@Valid @RequestBody PhotoVO photoVO){
        photoService.savePhoto(photoVO);
        return Result.ok();
    }

    /**
     * 更新照片
     * @param photoInfoVo
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "更新照片")
    @PutMapping("/admin/photo")
    public Result<String> uploadPhoto(@Valid @RequestBody PhotoInfoVO photoInfoVo){
        photoService.uploadPhoto(photoInfoVo);
        return Result.ok();
    }

    /**
     * 后台分页查询照片
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "分页查询照片")
    @GetMapping("/admin/photos")
    public Result<PageResult<PhotoBackDTO>> photoList(ConditionVO conditionVO){
        return Result.ok(photoService.photoList(conditionVO));
    }

    /**
     * 删除或恢复照片
     * @param deleteVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "删除或恢复照片")
    @PutMapping("/admin/photos")
    public Result logicDelete(@Valid @RequestBody DeleteVO deleteVO){
        photoService.logicDelete(deleteVO);
        return Result.ok();
    }

    /**
     * 彻底删除照片
     * @param photoIdList
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "彻底删除照片")
    @DeleteMapping("/admin/photos")
    public Result delete(@RequestBody List<String> photoIdList){
        photoService.deletePhotos(photoIdList);
        return Result.ok();
    }

    /**
     * 移动照片
     * @param photoVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "移动照片")
    @PostMapping("/admin/photo/move")
    public Result movePhoto(@Valid @RequestBody PhotoVO photoVO){
        photoService.movePhoto(photoVO);
        return Result.ok();
    }
}
